// pages/wareDetail/wareDetail.js
var app=getApp();
const util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    httpApiImg: app.globalData.httpApiImg,
    imageUrl: app.globalData.imageUrl,
    userInfo: '',
    imgUrls: [
      '../../img/1.jpg',
      // app.globalData.imageUrl+'bg.png',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg'
    ],
    actiItem: [],
    current:1,
    indicator: false,
    amount:1,
    showCommodity:false,
    animationData:{},
    canSelData:'',
    selIndex:'',
    productId:'',
    propertyId:'',
    goodsDetail:'',
    goodsImg:'',
    isMine:0,
    shopId: '',
    selected:'',
    addr:'',
    salesCount:0,
    clickRange: '',
    loadSuccess: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading()
    this.setData({
      addr: options.area,
      isMine: options.isMine,
      userInfo: JSON.parse(wx.getStorageSync('userInfo')),
      productId: options.id,
      shopId: options.shopId
    })
    this.getGoodInfo();
    this.getGoodsCount();
    this.getCommentInfo();
  },
  // 获取商品评论
  getCommentInfo: function () {
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'thingEvaluate/all',
      data: {
        shopId: _this.data.shopId,
        productId: _this.data.productId
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        for (var i = 0; i < res.data.data.length; i++) {
          res.data.data[i].createTime = app.formDate(new Date(res.data.data[i].createTime));
          res.data.data[i].evaluateUrl = res.data.data[i].evaluateUrl.split(",");
        }
        _this.setData({
          actiItem: res.data.data
        })
      }
    })
  },
  openAll: function(){
    wx.navigateTo({
      url: '/pages/assess/assess?shopId=' + this.data.shopId + '&productId=' + this.data.productId,
    })
  },
  // 查询商品销量
  getGoodsCount: function(){
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'shopOrder/getProductCount',
      data: {
        productId: _this.data.productId
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if(res.data.success){
          _this.setData({
            salesCount: res.data.data
          })
          
        }
      }
    })
  },
  showShopcar: function(){
    wx.redirectTo({
      url: '/pages/shopCart/shopCart',
    })
  },
  // 查询商品信息
  getGoodInfo: function(){
     var _this = this;
     wx.request({
       url: app.globalData.httpApiCar + 'shopThing/getOne',
       data: {
         id: _this.data.productId
       },
       header: {
         'content-type': 'application/json' // 默认值
       },
       success: function (res) {
         let key = 'goodsDetail.realThingBanerImg';
         let key1 = 'goodsDetail.realThingProductsImg';
         let key2 = 'goodsDetail.realThingShowImg';
         _this.setData({
            loadSuccess: true,
            goodsDetail: res.data.data,
            [key]: res.data.data.realThingBanerImg.split(","),
            [key1]: res.data.data.realThingProductsImg.split(","),
            [key2]: res.data.data.realThingShowImg.split(",")
        })
        wx.hideLoading()
         if (res.data.data.property){
           _this.setData({
             canSelData: res.data.data.property.split("-")
           })
        }
       }
     })
  },
  intervalChange: function (e) {
    this.setData({
      current: e.detail.current + 1
    })
  },
  // 减少购买数量
  reduceamount:function(e){
    if (this.data.amount>1){
      this.setData({ amount: this.data.amount-1})
    }
  },
  // 增加购买数量
  addamount: function (e) {
    this.setData({ amount: this.data.amount + 1 })
  },
  // 选择具体某件商品
  choiceTap:function(e){
    var selected = e.currentTarget.dataset.item;
    this.setData({
      selected: selected,
      selIndex: e.currentTarget.dataset.index
      
    })
  },
  // 选择商品确定按钮
  selSubmit: function (e) {
    if (this.data.canSelData != ''){
      this.data.propertyId = this.data.canSelData[this.data.selIndex];
      if(this.data.propertyId){
        if (this.data.isMine == 0){
          if (this.data.clickRange == 0){
            this.addCar()
          }
          else{
            this.submitFunc()
          }
        }
        this.closeCommodity();
      }
      else{
        wx.showToast({
          title: '请选择商品型号',
          icon: 'none',
          duration: 2000
        })
      }
    }
    else{
      this.closeCommodity()
    }
    
  },
  // 关闭购买弹窗
  closeCommodity:function(e){
    this.setData({ showCommodity: false })
  },
  showCommodity: function (e) {
    var animation = wx.createAnimation({
      duration: 6000,
      timingFunction: 'ease',
    })
    animation.translateY(0).step();
    this.data.animationData = animation;
    this.setData(
      {
         showCommodity: true ,
         animationData: animation.export()
      });
  },
  // 加入购物车
  addCar: function(){
    if (this.data.goodsDetail.thingNumber > 0){
      if (this.data.canSelData != '' && this.data.propertyId === '') {
        this.setData({
          clickRange: 0
        })
        this.showCommodity()
      }
      else {
        if (this.data.canSelData == '') {
          this.data.propertyId = '';
        }
        var _this = this;
        wx.request({
          url: app.globalData.httpApiCar + 'shoppingCart/save',
          data: {
            productId: _this.data.productId,
            propertyId: _this.data.propertyId,
            amount: _this.data.amount,
            userId: this.data.userInfo.id,
            shopId: _this.data.shopId
          },
          method: 'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
          },
          success: function (res) {
            if (res.data.success) {
              wx.showToast({
                title: '加入购物车成功',
                icon: 'success',
                duration: 2000
              })
            }
            else{
              wx.showToast({
                title: res.data.error,
                icon: 'none',
                duration: 2000
              })
            }
          }
        })
      }
    }
    else{
      wx.showToast({
        title: '库存不足',
        icon: 'none',
        duration: 2000
      })
    }
  },
  // 提交订单
  submitFunc: function(){
    if (this.data.goodsDetail.thingNumber > 0) {
      if (this.data.canSelData != '' && this.data.propertyId === '') {
        this.setData({
          clickRange: 1
        })
        this.showCommodity()
      }
      else {
        wx.redirectTo({
          url: '/pages/confirmOrder/confirmOrder?productId=' + this.data.productId + '&amount=' + this.data.amount + '&propertyId=' + this.data.propertyId,
        })
      }
    }
    else {
      wx.showToast({
        title: '库存不足',
        icon: 'none',
        duration: 2000
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})